package com.hj.javaweb.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hj.javaweb.entity.User;
import com.hj.javaweb.service.JsonSerializationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @Author : hujing
 * @Date: 2025/6/20 15:40
 * @Description:
 */
@RestController
public class UserController {
    
    @Autowired
    private JsonSerializationService jsonSerializationService;
    
    @PostMapping("/serialize")
    public String serialize(@RequestBody User user) throws JsonProcessingException {
        return jsonSerializationService.serialize(user);
    }
    
    @PostMapping("/deserialize")
    public User deserialize(@RequestBody String json) throws JsonProcessingException {
        return jsonSerializationService.deserialize(json);
    }
    
    @GetMapping("/demo")
    public String demo() throws JsonProcessingException {
        User user = new User();
        user.setName("张三");
        user.setAge(20);
        user.setHobbies(Arrays.asList("听音乐", "看电影", "打篮球"));

        String json = jsonSerializationService.serialize(user);
        System.out.println("序列化结果:"+ json);
        User dUser = jsonSerializationService.deserialize("{\"name\":\"张三\",\"hobbies\":[\"听音乐\",\"看电影\",\"打篮球\"]}");
        System.out.println("反序列化结果:"+dUser);
        return "查看控制台输出序列化和反序列化结果";
    }
}
